package exteditapp.command;

import exteditapp.FileDefault;
import exteditapp.SettingsFromFile;

/**
 * Initializes external editor behavior. Load system parameters. 
 * Create working file with appropriate url.
 * @author Arturas Smorgun, artuas@smorgun.lt
 */
public class CommandInit extends ICommand {

	/**
	 * Default constructor
	 */
	public CommandInit(){
		super();
	}

	public CommandInit(FileDefault file){
		setProgress(file);
	}
	
	public CommandInit(FileDefault file, String parameters){
		setProgress(file);
		setParams(parseParams(parameters));
	}
	
	/**
	 * Execution of initialization
	 * @return true on success and false otherwise
	 */
	public Boolean execute() {
		execution = true;
		
		//TODO implement initialization procedure
		//download settings and parameters
		SettingsFromFile settings = new SettingsFromFile(getProgress().getModule().getDocumentBase()+"config.ini");
		//TODO implement settings loading process
		settings.load();
		getProgress().getModule().setSettings(settings);
		getProgress().getModule().getParams();
		String fileName = settings.getParameter("fileName");
		getProgress().setDownloadURL(fileName);
		getProgress().setName(fileName.substring(fileName.lastIndexOf('/')+1, fileName.length()));
		
		getProgress().getCurrentState().setProgress(100);
		
		execution = false;
		file.getModule().getInvoker().setExecution(execution);
		return false;
	}

	/**
	 * Method for command execution invocation in separated thread
	 */
	public void run() {
		execute();
	}

}
